import { Button, View } from "@tarojs/components";

import "./index.scss";
import BackTitle from "@/components/BackTitle";

const Index = () => {
    const login = () => {
        setTimeout(function () {
            const { app } = window as any;
            var param: any = {};
            param.isReturnPage = false;
            if (typeof app.link.goLogin == "function") {
                app.link.goLogin(
                    function (res) {
                        alert(res.cityName);
                    },
                    function (error) {
                        console.log(error);
                    },
                    param
                );
            }
        }, 300);
    };

    const userInfo = () => {
        setTimeout(function () {
            const { app } = window as any;
            if (typeof app?.link?.getLoginInfo == "function") {
                app.link.getLoginInfo(
                    function (res) {
                        var userinfo = res;
                        let str = "";
                        Object.keys(userinfo).forEach((it) => {
                            str += it + ",\n";
                        });
                        // @ts-ignore
                        var encrypt = new JSEncrypt();
                        // @ts-ignore
                        encrypt.setPrivateKey(keyUtil.getPrivateKey());
                        var userId = encrypt.decrypt(userinfo.userId); //用户id
                        var userToken = encrypt.decrypt(userinfo.USER_TOKEN); //用户token
                        var userIdCard = encrypt.decrypt(userinfo.cardNum); //证件号码
                        var userName = encrypt.decrypt(userinfo.name); //用户姓名
                        var userMobile = encrypt.decrypt(userinfo.user_mobile); //用户姓名
                        alert(
                            "用户id：" +
                                userId +
                                "\n 用户token：" +
                                userToken +
                                "\n 证件号码：" +
                                userIdCard +
                                "\n 用户姓名：" +
                                userName +
                                "\n 用户手机：" +
                                userMobile +
                                "\n keys：" +
                                str
                        );
                    },
                    function () {
                        login();
                    }
                );
            }
        }, 300);
    };
    const userLocation = () => {
        setTimeout(function () {
            const { app } = window as any;
            if (typeof app?.getLocation == "function") {
                app?.getLocation(
                    function (res) {
                        alert(res.cityName);
                    },
                    function (error) {
                        console.log(error);
                    }
                );
            }
        }, 300);
    };
    const userPhoto = () => {
        setTimeout(function () {
            const { app } = window as any;
            if (typeof app?.link.getTakePictures == "function") {
                var param: any = {};
                param.pictureNum = 1;
                param.compressKB = 300; //指定压缩最小值,单位KB
                app.link.getTakePictures(function (res) {
                    var photos = res.images;
                    alert("success" + "图片数量:" + photos.length);
                }, param);
            }
        }, 300);
    };
    const openWeb = () => {
        setTimeout(function () {
            const { app } = window as any;
            if (typeof app?.link.goOpenNewPageOrExit == "function") {
                var param: any = {};
                param.exit = 1;
                param.closePrepage = false;
                param.url = "https://www.baidu.com"; //指定压缩最小值,单位KB
                app.link.goOpenNewPageOrExit(
                    param,
                    function (result) {
                        console.log("success" + result);
                    },
                    function (result) {
                        console.log("fail" + result);
                    }
                );
            }
        }, 300);
    };
    const openScan = () => {
        setTimeout(function () {
            const { app } = window as any;
            if (typeof app?.barcode.scan == "function") {
                app.barcode.scan(function (res) {
                    alert("success" + "url:" + res);
                });
            }
        }, 300);
    };
    const openPdf = () => {
        // www.meituhai.com/userupload/eva.webp
        setTimeout(function () {
            const { app } = window as any;
            var param: any = {};
            param.isSign = false;
            param.fileUrl =
                "https://mztapp.fujian.gov.cn:8192/mztAppFile/uploadfile/attach/4/20200617/588c7ebbccdd4b18a7f33d59acd9c20b.pdf";
            param.fileData = "";
            param.fileName = "下载地址的pdf 文件.pdf";
            if (typeof app?.link.showPDF == "function") {
                app.link.showPDF(function (res) {
                    alert("success" + "url:" + res);
                }, param);
            }
        }, 300);
    };
    const userVerify = () => {
        setTimeout(function () {
            const { app } = window as any;
            if (typeof app?.link.publicMethod == "function") {
                var param: any = {};
                param.methodCode = "faceCheck";
                param.isNeedInputUserInfo = "0"; //isNeedInputUserInfo:0:不需要输入用户信息，1：需要输入用户信息(不回传图片)
                param.isNeedIdentification = "1"; //0:不需要认证（回传图片）,1:需要认证(不回传图片)
                param.isCanEdit = true; ///false：不可修改ture：可修改
                param.cardNumber = "350322198912134396"; //身份证
                param.name = "郑勇锋"; //姓名
                param.userCardType = "111";
                param.userNationCode = "CHN";
                param.isShowTip = false;
                param.authCode = "";
                param.verifyCode = "VVV66666VSB";
                param.createTime = new Date().getTime();
                app.link.publicMethod(
                    function (result) {
                        alert(result.authVerifyCode);
                        alert("摘要：" + result.abstract);
                        alert(
                            "签名：" +
                                result.sign +
                                "\n 姓名：" +
                                result.name +
                                "\n 身份证：" +
                                result.cardNumber
                        );
                    },
                    function (result) {
                        alert("状态：" + result?.state + result?.msg);
                    },
                    param
                );
            }
        }, 300);
    };

    return (
        <View className="wrapper">
            <BackTitle title="测试" />
            <Button onClick={userInfo}>用户信息</Button>
            <Button onClick={userLocation}>定位城市</Button>
            <Button onClick={userPhoto}>拍照</Button>
            <Button onClick={userVerify}>人脸核验</Button>
            <Button onClick={openWeb}>打开web页面</Button>
            <Button onClick={openScan}>读取二维码</Button>
            <Button onClick={openPdf}>读取pdf</Button>
            <Button onClick={login}>登录</Button>
        </View>
    );
};

export default Index;
